/*
 * Copyright (C) 2012  E-dance Studio Harbin Institute of Technology 
 * Project:campusmobile
 * Author: 初楷博
 * Create Date: 2012-6-1下午10:49:48
 */
package hit.edance.campusmobile.chat.listener;

import java.util.Date;

import hit.edance.campusmobile.chat.activity.ActivityChat;
import hit.edance.campusmobile.chat.database.DBChatHistoryHelper;
import hit.edance.campusmobile.chat.model.ChatEntity;
import hit.edance.campusmobile.chat.model.ChatItem;
import hit.edance.campusmobile.chat.model.ChatMessage;
import hit.edance.campusmobile.chat.model.FriendsManager;
import hit.edance.campusmobile.chat.model.MyChatManager;
import hit.edance.campusmobile.chat.model.User;
import hit.edance.campusmobile.chat.service.CampusMobileService;
import hit.edance.campusmobile.chat.util.ConstantUtil;
import hit.edance.campusmobile.chat.util.StringUtil;

import org.jivesoftware.smack.Chat;
import org.jivesoftware.smack.MessageListener;
import org.jivesoftware.smack.packet.Message;

import android.os.Bundle;
import android.os.Handler;
import android.util.Log;

/**   
 * 聊天信息监听类
 * 
 * @Project campusmobile
 * @Package hit.edance.campusmobile.chat.listener
 * @Class MessageListenerEx
 * @Date 2012-6-1 下午10:49:48
 * @author 初楷博
 * @version 1.0
 * @since 1.0
 */
public class MessageListenerEx implements MessageListener {

	private Handler myhandler;
	public static final int CURRENT_CHAT = 0;
	private ChatEntity chatEntity;
	private static MessageListenerEx instance;
	private DBChatHistoryHelper db = ActivityChat.getDb();
	
	private MessageListenerEx(){
		Log.i("MessageListenerEx","i am starting...");
	}
	public static MessageListenerEx getInstance() {
		if(instance == null)
			instance = new MessageListenerEx();
	      return instance;
	  }
	/**
	 * @param myhandler the myhandler to set
	 */
	public void setMyhandler(Handler myhandler) {
		this.myhandler = myhandler;
	}
	/* @see org.jivesoftware.smack.MessageListener#processMessage(org.jivesoftware.smack.Chat, org.jivesoftware.smack.packet.Message)
	 *processMessage
	 */
	public void processMessage(Chat chat, Message message) {
		
		String jabberId = StringUtil.getLeftString(message.getFrom(), "/");
		String messagebody = message.getBody();
		db.insertChatHistoryItem(new ChatItem(ConstantUtil.JID, jabberId, ChatMessage.MESSAGE_FROM, new Date(), messagebody));
		String nickname = jabberId;
		if((chatEntity = MyChatManager.getChatEntityByJID(jabberId)) == null){
			for(User user : FriendsManager.getInstance().getFriends()){
				if(user.getJabberId().equals(jabberId))
					nickname = user.getNickname();
			}
			chatEntity = MyChatManager.addChatEntity(new ChatEntity(jabberId,nickname));
			chatEntity.setChat(CampusMobileService.connection.getChatManager().createChat(jabberId, null));
		}
		if(MyChatManager.getCurrentChattingActivity() != null){
			if(MyChatManager.getCurrentChattingActivity().isIAmTop()&&MyChatManager.getCurrentChattingActivity().getCurrentJabberId().equals(jabberId)){
				//直接刷新
				android.os.Message msg = myhandler.obtainMessage(CURRENT_CHAT);
				Bundle bd = new Bundle();
				bd.putString("msg", messagebody);
				msg.setData(bd);
				msg.sendToTarget();
				
			}else{
				chatEntity.getChatlist().add(new ChatMessage(ChatMessage.MESSAGE_FROM, messagebody));
				MyChatManager.getActivityTab().notifyChat(jabberId, chatEntity.getNickname(), messagebody);
				chatEntity.addUnreadMessageCount();
				//使用notafacation刷新
			}
		}else{			
			chatEntity.getChatlist().add(new ChatMessage(ChatMessage.MESSAGE_FROM, messagebody));
			MyChatManager.getActivityTab().notifyChat(jabberId, chatEntity.getNickname(), messagebody);
			chatEntity.addUnreadMessageCount();
			//使用notafacation刷新
		}
		
		
	}
}
